home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / os2 / edm0407s.zip / STAT3SRC.ZIP / EDITFILE.CPP < prev    next >
C/C++ Source or Header  |  1996-07-08  |  6KB  |  128 lines

  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // EDITFILE.CPP - description of AEdiFile
  4. //
  5. ////////////////////////////////////////////////////////////////////////////
  6.  
  7. #include "editfile.hpp"
  8.  
  9. #include <iexcbase.hpp>
  10. #include <ibexctxt.h>
  11.  
  12. #include "mymsg.h"
  13.  
  14. //**************************************************************************
  15. // AEditFile :: AEditFile - Constructor for AEditFile                      *
  16. //**************************************************************************
  17. AEditFile :: AEditFile (void)
  18.              : AFile ()
  19.  
  20. {
  21.   m_pFileContents = 0L;   // reset the variables
  22.  
  23.   m_ulFileSize = 0UL;
  24.  
  25.   m_bStopProcess = false;
  26. }
  27.  
  28. //**************************************************************************
  29. // AEditFile :: ~AEditFile - Destructor for AEditFile                      *
  30. //**************************************************************************
  31. AEditFile :: ~AEditFile (void)
  32.  
  33. {
  34.   delete [] m_pFileContents;  // delete the data buffer
  35. }
  36.  
  37. //**************************************************************************
  38. // AEditFile :: allocateDataBuffer - allocate the data buffer              *
  39. //**************************************************************************
  40. Boolean AEditFile :: allocateDataBuffer (void)
  41.  
  42. {
  43.   if(m_pFileContents)                                 // is the buffer already allocated
  44.      return (true);                                   // yes, return
  45.  
  46.   if(!isOpen ())                                      // is the file open?
  47.      return (false);                                  // no, return
  48.  
  49.   m_ulFileSize = getFileSize ();                      // get the file's size to know the buffer size
  50.  
  51.   if(!m_ulFileSize)                                   // no size?
  52.      throw m_ulFileSize;                              // throw an exception
  53.  
  54.   if(!(m_pFileContents = new char[m_ulFileSize]))     // allocate the buffer; if not successful
  55.      throw m_pFileContents;                           // throw an exception
  56.  
  57.   return (true);                                      // buffer allocated, return
  58. }
  59.  
  60. //**************************************************************************
  61. // AEditFile :: read - read the data into the buffer                       *
  62. //**************************************************************************
  63. APIRET AEditFile :: read (HWND hwndStatusLine)
  64.  
  65. {
  66.   ULONG ulBytesLeftToRead = m_ulFileSize;  // set the variables
  67.   ULONG ulReadAtBufferPos = 0;
  68.   APIRET ret;
  69.  
  70.   if(!m_pFileContents)                    // no buffer allocated?
  71.      throw m_pFileContents;               // throw an exception
  72.  
  73.   m_bStopProcess = false;                 // do not stop the process
  74.  
  75.   while(ulBytesLeftToRead)                // as long as there are still some bytes left to read
  76.         {
  77.          ret = AFile :: read (m_pFileContents + ulReadAtBufferPos,    // read a block of data
  78.                               (ulBytesLeftToRead > (ULONG) eBytesToRead ? eBytesToRead : ulBytesLeftToRead));
  79.          if(ret && ret != ERROR_MORE_DATA)                            // a serious error occured?
  80.             break;                                                    // yes, break the loop
  81.          if(m_bStopProcess)                                           // stop the process?
  82.             {                                                         // yes
  83.              m_bStopProcess = false;                                  // reset the flag
  84.              return (ERROR_MORE_DATA);                                // return
  85.             }
  86.          if(hwndStatusLine)                                           // is a handle of a status line passed?
  87.             WinPostMsg (hwndStatusLine, MYM_STATUS_PROCEED, 0, 0);    // yes, post the message
  88.          ulBytesLeftToRead -= getBytesRead ();                        // decrease the number of bytes left to read
  89.          ulReadAtBufferPos += getBytesRead ();                        // increase the position where to place the data
  90.         }
  91.  
  92.   return (ret);
  93. }
  94.  
  95. //**************************************************************************
  96. // AEditFile :: write - write the data from the buffer                     *
  97. //**************************************************************************
  98. APIRET AEditFile :: write (HWND hwndStatusLine)
  99.  
  100. {
  101.   ULONG ulBytesLeftToWrite = m_ulFileSize;    // set the variables
  102.   ULONG ulWriteFromBufferPos = 0;
  103.   APIRET ret;
  104.  
  105.   if(!m_pFileContents)                    // no buffer allocated?
  106.      throw m_pFileContents;               // throw an exception
  107.  
  108.   m_bStopProcess = false;                 // do not stop the process
  109.  
  110.   while(ulBytesLeftToWrite)               // as long as there are still some bytes left to write
  111.         {
  112.          if((ret = AFile :: write (m_pFileContents + ulWriteFromBufferPos, // write a block of data
  113.                                    (ulBytesLeftToWrite > (ULONG) eBytesToRead ? eBytesToRead : ulBytesLeftToWrite))))
  114.             break;                                                    // if not 0 == NO_ERROR returned, break the loop
  115.          if(m_bStopProcess)                                           // stop the process?
  116.             {                                                         // yes
  117.              m_bStopProcess = false;                                  // reset the flag
  118.              return (ERROR_MORE_DATA);                                // return
  119.             }
  120.          ulBytesLeftToWrite -= getBytesWritten ();                    // decrease the number of bytes left to write
  121.          ulWriteFromBufferPos += getBytesWritten ();                  // increase the (buffer-)position from where to write the data
  122.          if(hwndStatusLine && ulBytesLeftToWrite)                     // is a handle of a status line passed?
  123.             WinPostMsg (hwndStatusLine, MYM_STATUS_PROCEED, 0, 0);    // yes, post the message
  124.        }
  125.  
  126.   return (ret);
  127. }
  128.